﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using INIGui.Properties;
using IniLibrary;

namespace INIGui
{
    public partial class FrmMain : Form
    {

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (ActiveMdiChild != null)
            {
                Text = ActiveMdiChild.Text;
                openToolStripMenuItem.Enabled = true;
                saveToolStripMenuItem.Enabled = true;
                saveAsToolStripMenuItem.Enabled = true;
                mergeToolStripMenuItem.Enabled = true;
                closeToolStripMenuItem.Enabled = true;
            }
            else
            {
                Text = "INI Editor";
                openToolStripMenuItem.Enabled = false;
                saveToolStripMenuItem.Enabled = false;
                saveAsToolStripMenuItem.Enabled = false;
                mergeToolStripMenuItem.Enabled = false;
                closeToolStripMenuItem.Enabled = false;
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new frmIniWindow { MdiParent = this }.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog { Title = Resources.ofdTitle, Filter = Resources.ofdFilter })
            {
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                { (ActiveMdiChild as frmIniWindow).Open(ofd.FileName); Text = ActiveMdiChild.Text; }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (ActiveMdiChild as frmIniWindow).Save();
            Text = ActiveMdiChild.Text;
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var sfd = new SaveFileDialog { Title = Resources.sfdTitle, Filter = Resources.ofdFilter })
            {
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                { (ActiveMdiChild as frmIniWindow).Save(sfd.FileName); Text = ActiveMdiChild.Text; }
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveMdiChild.Close();
        }

        private void mergeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog { Title = "Merge INI file...", Filter = Resources.ofdFilter })
            {
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                { (ActiveMdiChild as frmIniWindow).Merge(ofd.FileName); }
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("INI Editor", "Developed by: formlesstree4", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
